<?php
/**
 * Controller pre domain
 * @author Tomas Srna
 * @package bakalarka
 */

class DomainController extends DomainAbstractController
{
	/**
	 * Index
	 */
	public function index()
	{
		if(array_key_exists(2, $this->args))
		{
			if($this->args[2] == 'data')
				return $this->data();
			else if($this->args[2] == 'go')
				return $this->go();
			else
			{
				return $this->domainDashboard();
			}
		}
		
		$t = Template::instance();
		
		$dv = new DynamicView();
		$dv->buttonAdd = false;
		$dv->primaryKeys = array('name');
		$dv->baseUrl = APPBASE."domain";
		
		// Nevieme to realne spocitat, aplikacna logika
		// ACL sa neda napodobnit SQL dotazom
		// Resp. da sa ale je to zlozite a ciastocne zbytocne.
		$dv->itemCount = 0;
			
		$t->dv = $dv;
		$t->dvw = new DynamicViewWriter($t->dv, $t);
		
		$t->title = L::selected()->domains;
		$t->text = "";
		
		$t->render('dynamic-view');
	}
	
	/**
	 * Data
	 */
	public function data()
	{
		$params = $_GET;
		
		$t = Template::instance();
		
		// Kontrola vstupov
		$by_possible = array("name", "active");
		
		$dt = new DynamicTable();
		$dt->primaryKeys = array('name');
		
		$this->itemCount = 0;
		$dt->buttons = array(
			new Button('server.png', L::selected()->administer, 
				"act('go', false, %params%)"));
		
		if(!$dt->check($params, $by_possible))
			$t->error = L::selected()->invalidInput;
			
		if(!property_exists($t, 'error'))
		{
			$dt->headers['name'] = L::selected()->name;
			$dt->headers['active'] = L::selected()->active;
			
			$dt->writers['active'] = new YesNoWriter();
			
			if(!array_key_exists('page', $params))
				$params['page'] = 1;
			
			$dt->itemCount = 0;
			
			if(Acl::instance()->allowed('domain:*'))
			{
				$select = Selector::select()
							->from(DomainModel::struct());
			}
			else
			{
				$select = Selector::select()
							->from(DomainModel::struct())
							->where('name in (?)', new Expression(
								Selector::select(
									new Expression('substr(resource,8)'))
									->from(AclModel::struct())
									->where(
										'(role = ? OR role = ?)'.
										' AND substr(resource, 1,7) '.
										' = ?',
										Auth::instance()->getUser()
											->username,
										'@'.Auth::instance()->getUser()
											->group,
										'domain:'
									)
							));
				
				
			}
			
			$dt->itemCount = Selector::select('count(*) as c')
								->from(new Expression($select))
								->fetch()->current()->c;
			
			$select = $select->page($params['page'])
								->by($params);
			$domains = $select->fetch();
			
			$dt->data = $domains;
			
			$t->dtw = new DynamicTableWriter($dt, $t);
		}
			
		$t->dt = $dt;
		$t->render('dynamic-table');
	}

	/**
	 * Redirect vyberu domeny
	 */
	public function go()
	{
		if(array_key_exists('name', $_GET))
		{
			header('Location: ' . APPBASE . 'domain/' . $_GET['name']);
		}
		else
			$this->autoredir();
		exit();
	}
	
	/**
	 * Prehlad domeny
	 */
	public function domainDashboard()
	{
		$t = Template::instance();
		
		$t->title = sprintf(L::selected()->domain_s, $this->args[2]);
		
		$t->plusmenu = $this->plusmenu;
		$t->plusmenu_title = $this->plusmenu_title;
		
		$active = Selector::select('active')->from(DomainModel::struct())
					->where('name = ?', $this->domain)->fetch()->current()
					->active;
		
		if(!$active)
			$t->error = L::selected()->domainDisabled;
		
		$t->text = '<p>'.L::selected()->domainValid.'</p>'.
			'<div class="intersection">'.$this->plusmenu.'</div>';
		
		$t->render();
	}
	
}